'''
Created on Oct 3, 2012

@author: Himanshu

'''
from dev.sampler import ReSampler
from dev.parser import Parser
from dev.classifiers import oneNN
from dev.testing import LOOCV
import os
import cPickle as pickle
from dev.featureset import gesture
from dev.featureset import gestureSet

def fillStartTimes(startTimes):
    fullStartTimes = []
    for startTime in startTimes:
        for i in range(0,10):
            fullStartTimes.append(startTime + i*15)
    return fullStartTimes

def readAllPickles():
    path = os.getcwd() + "\data"
    ngestureSet = gestureSet()
    for fileName in os.listdir(path):
        if (os.path.splitext(fileName)[1] == ".pkl"):
            pklFile = open(path + '\\' + fileName, 'rb')
            gestureName = pickle.load(pklFile)
            noGestures = pickle.load(pklFile)
            tup1 = pickle.load(pklFile)
            if ngestureSet.returnGestureSet().has_key(gestureName):
                oldGesture = ngestureSet.removeGesture(gestureName)
                oldGesture.addInstances(tup1[1])
                ngestureSet.addGesture(oldGesture)
            else:
                nGesture = gesture(tup1[1], gestureName)
                ngestureSet.addGesture(nGesture)
    return ngestureSet

if __name__ == '__main__':
    "Parsing Data Files"
    
    """parser = Parser()
    globalStart = 1350464598 + 582
    globalEnd = globalStart + 20*60 + 30
    parser.parse('17-Oct-2012\AccelerometerSensorProbe.csv', globalStart - 10, globalEnd + 10)
    "Build pickles of features"
    parser.buildPickles(globalStart)"""
    
    "Re-sampling on a regular grid)"
    meshSize = 10**7
    sampler = ReSampler()
    sampler.reSampleAll(meshSize)

    "read all gestures into memory and assign them scores"
#    gestSet = readAllPickles()
#    gestureNames = ['Orient', 'Slap', 'Triangle', 'Window_Open', 'Window_Close', 'Fire_On', 'Fire_Off', 'Throw_Money', 'Swing_Phone',
#                    'W', 'Tap_phone']
#    gestureScores = [0.607, 0.818, 0.555, 0.173, 0.812, 0, 0.722, 0.527, 0.489, 0.247, 0.597]
#    i = 0
#    for name in gestureNames:
#        gest = gestSet.removeGesture(name)
#        gest.setScore(gestureScores[i])
#        gestSet.addGesture(gest)
#        i += 1
#        
#    Slap = gestSet.removeGesture('Slap')
#    slapInstances = Slap.returnInstances()
#    Orient = gestSet.removeGesture('Orient')
#    orientInstances = Orient.returnInstances()
#    
#    
#    trainingSet = gestSet.returnGestureSet()
#    featureList = []
#    scoreList = []
#    for key in trainingSet.keys():
#        gesture = trainingSet[key]
#        for i in range (gesture.returnNoInstances()):
#            featureList.append(gesture.returnInstances()[i])
#            scoreList.append(gesture.returnScore())
#    "Classify"
#    #print scoreList
#    classifier = oneNN()
#    classifier.setTrainingSet(featureList)
#    classifier.setTargetVector(scoreList)
#    print "----------SLAP-----------"
#    sumi = 0
#    i = 0
#    for instance in slapInstances:
#        score = classifier.classify(instance)
#        sumi += score
#        i +=1
#        p = 1.0*i/slapInstances.__len__()
#        print p, " % done!"
#    avg = sumi/slapInstances.__len__()
#    print avg
#    print Slap.returnScore()
#    print avg - Slap.returnScore()
#    
#    print "----------Orient------------"
#    
#    sumi = 0
#    i = 0
#    for instance in orientInstances:
#        score = classifier.classify(instance)
#        sumi += score
#        i +=1
#        p = 1.0*i/orientInstances.__len__()
#        print p, " % done!"
#    avg = sumi/orientInstances.__len__()
#    print avg
#    print Orient.returnScore()
#    print avg - Orient.returnScore()
#    
#    
#    """test = LOOCV()
#    test.setFeatureSet(featureList)
#    test.setTargetVector(scoreList)
#    computedClass = test.experiment(classifier)
#    print computedClass
#    print test.countErrors(computedClass)"""
    


                
                
                
                
                